<?php 

function news_feed_login_assign_user ($user_id, $user_pass) {
	if (!isset($user_id) || !isset($user_pass)) {
		return;
	}
	$user = get_user_by('id', $user_id);
	
	if (empty($user)){
		return;
	}
	
	$credentials['user_login'] = $user->user_login;
	$credentials['user_password'] = $user_pass;
	
	if (!is_user_logged_in()) {
		$user = wp_signon( $credentials, false );
		set_current_user($user->ID, $user->name);
	}
	
	if (!is_user_logged_in()) {
		return false;
	}
	
	return true;
}

add_action('tml_new_user_activated', 'news_feed_login_assign_user', 10, 2);

function news_feed_activation_complete() {
	if (!isset($_REQUEST['activation']) || $_REQUEST['activation'] != 'complete') {
		return;
	}
	$redirect_to = add_query_arg('action', 'profile', wp_login_url());
	
	if (is_user_logged_in()) {
		wp_redirect($redirect_to);
	}
}

add_action ('login_form_login', 'news_feed_activation_complete');

function news_feed_get_profile_url () {
	return home_url('profile');
}

add_action ('login_form_register', 'news_feed_login_form_register');

function news_feed_login_form_register() {
	if (isset($_POST['user_login']) && isset($_POST['user_email']) && isset($_POST['redirect_to'])) {
		$_POST['redirect_to'] = add_query_arg(array('user_login'=>urlencode($_POST['user_login']), 'user_email'=>urlencode($_POST['user_email'])), $_POST['redirect_to']);
	}
}

add_action ('login_form_lostpassword', 'news_feed_login_form_lostpassword');

function news_feed_login_form_lostpassword() {
	if (isset($_POST['user_login']) && isset($_POST['redirect_to'])) {
		if ( strpos( $_POST['user_login'], '@' ) ) {
			$user_data = get_user_by_email( trim( $_POST['user_login'] ) );
			if ( empty( $user_data ) )
				return true;
		} else {
			$login = trim( $_POST['user_login'] );
			$user_data = get_user_by( 'login', $login );
			if ( empty( $user_data ) )
				return true;
		}
		$_POST['redirect_to'] = add_query_arg(array('user_login'=>urlencode($user_data->data->user_email)), $_POST['redirect_to']);
		$_REQUEST['redirect_to'] = $_POST['redirect_to'];
	}
}

add_action ('tml_title', 'news_feed_tml_title', 10, 2);

function news_feed_tml_title($title, $action) {
	switch ( $action ) {
		case 'register':
			$title = __('unregister?', 'theme-my-login');
			break;
		case 'lostpassword':
			$title = __('lostpassword?', 'theme-my-login');
			break;
		default:
			break;
	}
	return $title;
}

function news_feed_new_user_activation_notification( $user_login, $user_email, $key = '' ) {
	global $wpdb, $current_site;

	if ( empty( $key ) ) {
		$key = $wpdb->get_var( $wpdb->prepare( "SELECT user_activation_key FROM $wpdb->users WHERE user_login = %s", $user_login ) );
		if ( empty( $key ) ) {
			$key = wp_generate_password( 20, false );
			$wpdb->update( $wpdb->users, array( 'user_activation_key' => $key ), array( 'user_login' => $user_login ) );
		}
	}

	if ( is_multisite() ) {
		$blogname = $current_site->site_name;
	} else {
		// The blogname option is escaped with esc_html on the way into the database in sanitize_option
		// we want to reverse this for the plain text arena of emails.
		$blogname = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );
	}

	$activation_url = add_query_arg( array( 'action' => 'activate', 'key' => $key, 'login' => rawurlencode( $user_login ) ), wp_login_url() );

	$title    = sprintf( __( '[%s] Activate Your Account', 'theme-my-login' ), $blogname );
	$message  = sprintf( __( 'Thanks for registering at %s! To complete the activation of your account please click the following link: ', 'theme-my-login' ), $blogname ) . "\r\n\r\n";
	$message .=  $activation_url . "\r\n";


	return wp_mail( $user_email, $title, $message );
}


add_action ('personal_options_update', 'news_feed_personal_options_update');

function news_feed_personal_options_update ($user_ID) {
	if (isset($_POST['nickname'])) {
		$_POST['display_name'] = $_POST['nickname'];
	}
	$user_data = get_user_by('id', $user_ID);
	if (!isset($_POST['email'])) {
		$_POST['email'] = $user_data->data->user_email;
	}
}

add_action ('tml_request_profile', 'news_feed_tml_request_profile', 100);

function news_feed_tml_request_profile() {
	wp_enqueue_style( 'news_feed_password-strength', get_stylesheet_directory_uri().'/themed-profiles.css' ) ;
}

add_action ('tml_request_update', '__return_true');
	
add_action ('user_profile_update_errors', 'news_feed_user_profile_update_errors', 10, 3);

function news_feed_user_profile_update_errors(&$errors, $update, &$user) {
	if ($update && get_user_option ( 'default_password_nag' )) {
		if (!( $errors->get_error_message () )) {
			if (empty ( $user->nickname )) {
				$errors->add ( 'invalid_nickname', __ ( '<strong>ERROR</strong>: Please enter your nickname.' , 'theme-my-login'), array (
						'form-field' => 'nickname' 
				) );
			}
		} 
		if (!( $errors->get_error_message () )) {
			if (empty ( $_POST ['pass1'] ))
				$errors->add ( 'pass', __ ( '<strong>ERROR</strong>: Please enter your password.' ), array (
						'form-field' => 'pass1' 
				) );
			elseif (empty ( $_POST ['pass2'] ))
				$errors->add ( 'pass', __ ( '<strong>ERROR</strong>: Please enter your password twice.' ), array (
						'form-field' => 'pass2' 
				) );
		}
	}
}

add_filter('login_errors', 'news_feed_login_errors');
add_filter('login_messages', 'news_feed_login_messages');
function news_feed_login_errors($errors) {
	if (! empty ( $errors ))
		echo "<div class=error>" . $errors . "</div>";
}
function news_feed_login_messages($messages) {
	if (! empty ( $messages ))
		echo "<div class=message>" . $messages . "</div>";
}

add_action ('wp_head', 'news_feed_wp_head');

function news_feed_wp_head () {
	if (get_user_option ( 'default_password_nag' )) {
		echo "
					<script type='text/javascript'> 
						function validate_required(field, error_field)
						{
							with (field)
							{
								if (value == null || value =='')
								{
									document.getElementById(error_field).style.display='inline';
									return false;
								} else return true;
							}
						}
						function compare_password(field1,field2,error_field)
						{
							var field1value, field2value;
							with (field1)
							{
								field1value = value;
							}
							with (field2)
							{
								field2value = value;
							}
							if (field1value != field2value)
							{
								document.getElementById(error_field).style.display='inline';
								return false;
							}
						}
						
						function validate_form (form)
						{
							with(form)
							{
								if (validate_required(nickname, 'nickinfo') == false) 
								{
									nickname.focus();
									return false;
								}
								if (validate_required(pass1,  'pass1info') == false) 
								{
									pass1.focus();
									return false;
								}
								if (compare_password(pass1, pass2,  'pass2info') == false)
								{
									pass2.focus();
									return false;
								}
							}		
						}
					</script> ";
	}
	
}
